package zmee.bid.oa;

import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.args.AfterOperationArgs;
import kd.bos.servicehelper.AttachmentServiceHelper;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.bos.servicehelper.user.UserServiceHelper;
import zmee.bid.formplugin.SrcDecisionFormPlugin;
import zmee.entity.FrontWorkflowRequestInfoin0;
import zmee.util.OARequestUtil;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
* @author lijiawang
* @Date 2022/12/14 10:22
* @Description:定标提交进OA流程
*/
public class SrcDecisionOA extends AbstractOperationServicePlugIn {
    private Log log= LogFactory.getLog(SrcDecisionFormPlugin.class);
    @Override
    public void afterExecuteOperationTransaction(AfterOperationArgs e) {
        String key = e.getOperationKey();
        DynamicObject[] dataEntities = e.getDataEntities();
        if("submit".equals(key)){
            for (DynamicObject ent:dataEntities) {
                DynamicObject decision = BusinessDataServiceHelper.loadSingle(ent.getPkValue(), ent.getDynamicObjectType().getName());
                DynamicObject sourceclass = decision.getDynamicObject("sourcetype");
                DynamicObject org = decision.getDynamicObject("org");
                switch (sourceclass.getString("number")) {
                   case "C010405":
                   case "C010406":
                   case "C010418":
                   case "C010415":
                       if(org.getBoolean("zmee_oa")){
                           getWorkflowRequest(decision, "环能-招标采购定标审核流程", "352772");
                       }else {
                           getWorkflowRequest(decision, "环能实业-招标采购定标审核流程", "272772");
                       }
                    break;
                   default:
                       break;
                }
            }
        }
    }

    private void getWorkflowRequest(DynamicObject decision,String workflowName,String workflowId){
        String name = decision.getDynamicObjectType().getName();
        String id = decision.getPkValue().toString();
        Map info = new HashMap();
        info.put("resource", "优采云");
        DynamicObject user = decision.getDynamicObject("oasqr");
        info.put("usderid", user.getString("username"));
        info.put("accountType", 1);
        info.put("name", workflowName);//请求标题
        info.put("workflowName", workflowName);//工作流标题
        info.put("workflowId", workflowId);//工作流ID
        //info.put("isnextflow", 0);
        List<Map<String, Object>> attachmentpanels = AttachmentServiceHelper.getAttachments(name, id, "attachmentpanel");
        log.info("===attachmentpanels===" + attachmentpanels);
        Map<String, Object> mainfieldMap;
        List<Map<String, String>> stringListMap = OAutil.urlList(attachmentpanels, "zbxj");
        if("352772".equals(workflowId)){
            mainfieldMap=OAutil.getMainfieldParam(decision,user);
        }else {
             mainfieldMap = OAutil.getMainfieldMap(decision);
        }
        //单据数据转请求数据
        FrontWorkflowRequestInfoin0 requestInfoin0 = OARequestUtil.getFrontWorkflowRequestInfoin0(info, mainfieldMap, stringListMap);
        //请求参数
        String body = OARequestUtil.workflowRequestBody(requestInfoin0);
        log.info("==定标-body==" + body);
        //调用OA审批接口
        String request = OARequestUtil.doCreateWorkflowRequest(body);
        String processing = OARequestUtil.requestProcessing(request);
        log.info("==定标-request==" + processing);
        decision.set("oaflow",processing);
        SaveServiceHelper.update(decision);
    }

}
